package org.agile2soft.nexoma.tapestry.encoder;

import org.agile2soft.nexoma.controller.InternalUserController;
import org.agile2soft.nexoma.entity.InternalUser;
import org.apache.tapestry5.EventContext;
import org.apache.tapestry5.ValueEncoder;
import org.hibernate.SessionFactory;

import br.com.arsmachina.module.service.PrimaryKeyTypeService;
import br.com.arsmachina.tapestrycrud.encoder.Encoder;
import br.com.arsmachina.tapestrycrud.hibernate.encoder.HibernatePrimaryKeyEncoder;

public class InternalUserEncoder
       extends HibernatePrimaryKeyEncoder<InternalUser,String>
       implements Encoder<InternalUser,String>{

//  public InternalUserEncoder(Class<InternalUser> entityClass,
//  InternalUserController controller,
//  SessionFactory sessionFactory,
//  PrimaryKeyTypeService primaryKeyTypeService) {
//super(entityClass, controller, sessionFactory,primaryKeyTypeService);
//}
    public InternalUserEncoder(Class<InternalUser> entityClass,
            InternalUserController controller,
            SessionFactory sessionFactory) {
          super(entityClass, controller, sessionFactory);
    }

    public Class<String> getKeyType() {
        return String.class;
    }

    public String toClient(InternalUser value) {
        final String key = toKey(value);
        return key !=null ? key.toString() : "";
    }

    public ValueEncoder<InternalUser> create(Class<InternalUser> type) {
        return this;
    }

    public Object toActivationContext(InternalUser internalUser) {
        return internalUser.getLogin();
    }

    public InternalUser toObject(EventContext context) {
        InternalUser user = null;
        if ( context.getCount() > 0 ) {

            final String id = context.get(String.class, 0);
            user = toValue(id);
        }
        return user;
    }

    public String toLabel(InternalUser internalUser) {
        return internalUser.getLogin();
    }

}
